package wechat

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"vrcm/src/vrcm/app/admin/service/dto/wechat"
	wechatService "vrcm/src/vrcm/app/admin/service/wechat"
	"vrcm/src/vrcm/common/apis"
)

type Wechat struct {
	apis.Api
}

// GetWechatToken 获取微信token
func (s Wechat) GetWechatToken(c *gin.Context) {
	req := wechat.GetWechatTokenReq{}
	wechat := wechatService.WechatService{}
	err := s.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&wechat.Service).
		Errors
	if err != nil {
		s.Logger.Error(err)
		s.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := wechat.GetWechatToken(&req)
	if err != nil {
		s.Error(apis.GetWechatAccessTokenError.Code, err, fmt.Sprintf("获取微信token失败 %s", err.Error()))
		return
	}
	s.OK(data, "获取微信token成功")
}

// GetWechatUserInfo 获取微信用户
func (s Wechat) GetWechatUserInfo(c *gin.Context) {
	req := wechat.GetWechatUserInfoReq{}
	wechat := wechatService.WechatService{}
	err := s.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&wechat.Service).
		Errors
	if err != nil {
		s.Logger.Error(err)
		s.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := wechat.GetWechatUserInfo(&req)
	if err != nil {
		s.Error(apis.GetWechatUserInfoError.Code, err, fmt.Sprintf("获取微信用户失败 %s", err.Error()))
		return
	}
	s.OK(data, "获取微信用户失败成功")
}

// GetWechatShareTicket 获取微信分享ticket
func (s Wechat) GetWechatShareTicket(c *gin.Context) {
	req := wechat.GetWechatTokenReq{}
	wechat := wechatService.WechatService{}
	err := s.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&wechat.Service).
		Errors
	if err != nil {
		s.Logger.Error(err)
		s.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := wechat.GetWechatShareTicket(&req)
	if err != nil {
		s.Error(apis.GetWechatShareTicketError.Code, err, fmt.Sprintf("获取微信分享ticket失败 %s", err.Error()))
		return
	}
	s.OK(data, "获取微信分享ticket成功")
}

func (s Wechat) CorpIdToOpenCorpId(c *gin.Context) {
	req := wechat.ToOpenCorpIdReq{}
	wechat := wechatService.WechatService{}
	err := s.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&wechat.Service).
		Errors
	if err != nil {
		s.Logger.Error(err)
		s.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := wechat.CorpIdToOpenCorpId(&req)
	if err != nil {
		s.Error(apis.GetWechatShareTicketError.Code, err, fmt.Sprintf("获取OpenCorpId失败 %s", err.Error()))
		return
	}
	s.OK(data, "获取OpenCorpId成功")
}
